﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PureLink.BL;
using PureLink.DO;

namespace PureLink.AdminCP.PageUC
{
    public partial class ucDoiTac : System.Web.UI.UserControl
    {
        private cmsDoiTacDO obj = new cmsDoiTacDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["IDDT"]!=null)
            {
                obj.DoiTacID = int.Parse(Request.QueryString["IDDT"].ToString());
                init();
            }
        }
        public void init()
        {
            obj = new cmsDoiTacBL().Select(obj);
            txtTenDT.Text = obj.TenDoiTac;
            hplImage.NavigateUrl = "~/Media/" + obj.ImageURL;
            txtURL.Text = obj.sURL;
        }
        private string UploadFile(FileUpload fulImage)
        {
            if (fuImage.HasFile)
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTenDT.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
        public void initObj()
        {
            obj.TenDoiTac = txtTenDT.Text;
            obj.ImageURL = UploadFile(fuImage);
            obj.sURL = txtURL.Text;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObj();
            if(obj.DoiTacID>0)
            {
                new cmsDoiTacBL().Update(obj);
                Functions.Alert("Cập nhật thành công!!","Default.aspx?Page=ListDoiTac");
            }
            else
            {
                new cmsDoiTacBL().Insert(obj);
                Functions.Alert("Cập nhật thành công!!","Default.aspx?Page=ListDoiTac");
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=ListDoiTac");
        }
    }
}